const staticSrc = "http://127.0.0.1:3038/emotion/";
const staticSrcSound = 'http://127.0.0.1:3038/jj-sound/changnei-yinxiao/';
const tags = 'self_1,self_2,self_3,self_4,other_1,other_2,other_3,other_4,other_3_toggle';
let timer = null;
let i = 1;

export function getresources(tags) {
  let set = {};
  let list = tags.split(",");
  list.forEach(key => {
    set[key] = {};
    switch (key) {
      case 'other_1':
        set[key]['src'] = `${staticSrc}other_emotion_gate_`;
        set[key]['length'] = 24;
        set[key]['speed'] = 100;
        break;
      case 'other_2':
        set[key]['src'] = `${staticSrc}other_emotion_rose_`;
        set[key]['length'] = 19;
        set[key]['speed'] = 100;
        break;
      case 'other_3':
        set[key]['src'] = `${staticSrc}other_emotion_money_fadeout_`;
        set[key]['length'] = 12;
        set[key]['speed'] = 300;
        break;
      case 'other_4':
        set[key]['src'] = `${staticSrc}other_emotion_kiss_`;
        set[key]['length'] = 8;
        set[key]['speed'] = 200;
        break;
      case 'self_1':
        set[key]['src'] = `${staticSrc}self_emotion_cool_`;
        set[key]['length'] = 8;
        set[key]['speed'] = 200;
        break;
      case 'self_2':
        set[key]['src'] = `${staticSrc}self_emotion_crazy_`;
        set[key]['length'] = 8;
        set[key]['speed'] = 200;
        break;
      case 'self_3':
        set[key]['src'] = `${staticSrc}self_emotion_cry_`;
        set[key]['length'] = 8;
        set[key]['speed'] = 200;
        break;
      case 'self_4':
        set[key]['src'] = `${staticSrc}self_emotion_laugh_`;
        set[key]['length'] = 8;
        set[key]['speed'] = 200;
        break;
      default:
        set['other_3_toggle']['src'] = `${staticSrc}other_emotion_money_fly_`;
        set['other_3_toggle']['length'] = 8;
        set['other_3_toggle']['speed'] = 200;
        // console.log("待实现功能");
        break;
    }
  })
  return function (index, img) {
    if(!img){
      return set[index]['src'] + 1 + ".png";
    }
    return new Promise((resolve, reject) => {
      window.clearInterval(timer);
      timer = window.setInterval(() => {
        img.src = set[index]['src'] + i + ".png";
        if (i >= set[index]['length']) {
          window.clearInterval(timer);  //清除定时器
          i = 1;  //重置索引
          resolve(true);  //标志动画完成
        } else {
          i++
        }
      }, set[index]['speed'])
    })
  }
}

export let changeImages = getresources(tags)

export function setImgSrc(tags) {
  let set = {};
  let list = tags.split(",");
  list.forEach(key => {
    set[key] = {};
    switch (key) {
      case 'other_1':
        set[key]['img'] = `${staticSrc}other_emotion_gate_lock.png`
        set[key]['sound'] = `${staticSrcSound}lordunion_emotion_gate.mp3`
        break;
      case 'other_2':
        set[key]['img'] = `${staticSrc}other_emotion_icon_rose.png`
        set[key]['sound'] = `${staticSrcSound}lordunion_emotion_rose.mp3`
        break;
      case 'other_3':
        set[key]['img'] = `${staticSrc}other_emotion_icon_money.png`
        set[key]['sound'] = `${staticSrcSound}lordunion_emotion_money.mp3`
        break;
      case 'other_4':
        set[key]['img'] = `${staticSrc}other_emotion_icon_kiss.png`
        set[key]['sound'] = `${staticSrcSound}lordunion_emotion_kiss.mp3`
        break;
      default:
        // console.log("不是集合里面的数据，尴尬了~");
        break;
    }
  })
  return function (val) {
    val = val.toString()
    return set[val]
  }
}

export let getImgSrc = setImgSrc(tags)

// img,src,length,speed
// img,index
// export function changeImg(obj) {
//   let timer = null;
//   let i = 1;
//   timer = window.setInterval(() => {
//     obj.img.src = obj.src + i + ".png";
//     i >= obj.length ? window.clearInterval(timer) : i++;
//   }, obj.speed)
// }